/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.Serializable;

import org.jfree.chart.renderer.PaintScale;

/**
 * 
 * @author Bunkowski
 */
public class WBRG_PaintScale implements Serializable, PaintScale {

	public static final long serialVersionUID = 1L;

	private final double low, high;
	private Color[] colors;

	public WBRG_PaintScale(double low, double high) {
		this.low = low;
		this.high = high;

		Color[] gradientColors = new Color[] { Color.white, Color.blue, Color.red, Color.yellow };
		int nrOfGradients = gradientColors.length - 1;
		int resolution = nrOfGradients * 256;
		int pixelPerGradient = resolution / nrOfGradients;

		BufferedImage img = new BufferedImage(resolution, 1, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2d = (Graphics2D) img.getGraphics();

		for (int i = 0; i < nrOfGradients; i++) {
			Rectangle2D rect = new Rectangle2D.Double(i * pixelPerGradient, 0, (i + 1) * pixelPerGradient, 1);
			GradientPaint gp = new GradientPaint(i * pixelPerGradient, 1, gradientColors[i],
					(i + 1) * pixelPerGradient, 1, gradientColors[i + 1]);
			g2d.setPaint(gp);
			g2d.fill(rect);
		}
		colors = new Color[img.getWidth()];
		for (int i = 0; i < img.getWidth(); i++) {
			colors[i] = new Color(img.getRGB(i, 0));
		}

		// //gradient 1
		// Rectangle2D rect1 = new Rectangle2D.Double(0, 0, resolution / nrOfGradients, 1);
		// GradientPaint gp1 = new GradientPaint(
		// 0, 1, Color.white,
		// resolution / nrOfGradients, 1, Color.blue);
		// g2d.setPaint(gp1);
		// g2d.fill(rect1);
		//
		// //gradient 2
		// Rectangle2D rect2 = new Rectangle2D.Double(resolution / nrOfGradients, 0, resolution / nrOfGradients, 1);
		// GradientPaint gp2 = new GradientPaint(
		// resolution / nrOfGradients, 1, Color.blue,
		// resolution / nrOfGradients * 2, 1, Color.red);
		// g2d.setPaint(gp2);
		// g2d.fill(rect2);
		//
		// //gradient 3
		// Rectangle2D rect3 = new Rectangle2D.Double(resolution / nrOfGradients * 2, 0, resolution / nrOfGradients, 1);
		// GradientPaint gp3 = new GradientPaint(
		// resolution / nrOfGradients * 2, 1, Color.red,
		// resolution / nrOfGradients * 3, 1, Color.yellow);
		// g2d.setPaint(gp3);
		// g2d.fill(rect3);
	}

	public double getLowerBound() {
		return this.low;
	}

	public double getUpperBound() {
		return this.high;
	}

	public Paint getPaint(double value) {
		value *= high;

		if (value < 0) {
			return colors[0];
		}
		if (value > 1) {
			return colors[colors.length - 1];
		}
		return colors[(int) (value * (colors.length - 1))];

	}
}